package com.chao.authcenter.exception.handler;

import com.constant.ResultCode;
import com.exception.AuthenticationException;
import org.assertj.core.api.ThrowableAssert;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

import java.util.HashMap;
import java.util.Map;

/**
 * auth2.0 登录认证异常处理
 */
public class MyWebResponseExceptionTranslator implements WebResponseExceptionTranslator {
    @Override
    public ResponseEntity translate(Exception e) throws Exception {
        Map<String,Object> map = new HashMap();
        map.put("code",ResultCode.FAILED.getCode());
        //自定义的认证异常
        if(e instanceof InternalAuthenticationServiceException){
            map.put("message",e.getCause().getMessage());
        }else{
            map.put("message",e.getMessage());
        }
        return ResponseEntity.badRequest().body(map);
    }
}
